package com.hibase.core.log.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hibase.core.log.entity.MdsOperationLog;
import com.hibase.core.log.entity.vo.MdsOperationLogSearchVO;
import com.hibase.core.log.mapper.MdsOperationLogMapper;
import com.hibase.core.log.service.MdsOperationLogService;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author chenfeng
 * @since 2021-03-10
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class MdsOperationLogServiceImpl extends BaseServiceImpl<MdsOperationLogMapper, MdsOperationLog> implements MdsOperationLogService {

    /**
     * 查询操作日志列表
     */
    @Override
    public PageInfo<MdsOperationLog> selectMdsOperationLogList(MdsOperationLogSearchVO searchVO) {
        QueryWrapper<MdsOperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(searchVO.getType()),"type",searchVO.getType())
                .eq(StrUtil.isNotBlank(searchVO.getSource()),"source",searchVO.getSource())
                .eq(StrUtil.isNotBlank(searchVO.getOperator()),"operator",searchVO.getOperator())
                .in(CollUtil.isNotEmpty(searchVO.getTypes()),"type",searchVO.getTypes())
                .orderByDesc("id")
                ;
        return super.page(searchVO, queryWrapper);
    }

    /**
     * 改变操作日志记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMdsOperationLog(MdsOperationLog entity) {

        super.store(entity);
    }

    /**
     * 查询操作日志详情
     */
    @Override
    public MdsOperationLog viewMdsOperationLog(String id) {

        MdsOperationLog entity = super.getById(id, true);

        return entity;
    }

    /**
     * 删除操作日志记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMdsOperationLog(String id) {

        super.deleteBy(id);
    }

    @Override
    public List<MdsOperationLog> selectList(MdsOperationLogSearchVO searchVO) {
        QueryWrapper<MdsOperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(searchVO.getType()),"type",searchVO.getType())
                    .eq(StrUtil.isNotBlank(searchVO.getSource()),"source",searchVO.getSource())
                    .eq(StrUtil.isNotBlank(searchVO.getOperator()),"operator",searchVO.getOperator())
                    .last("limit 5");
        return super.list(queryWrapper);
    }
}
